package com.huawei.hgip.paintgame.widget;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import com.huawei.hgip.paintgame.R;
import com.huawei.hgip.paintgame.m.data.UserProfile;
import com.huawei.hgip.paintgame.util.Communicate;
import com.huawei.hgip.paintgame.util.PGUtil;
import com.huawei.hgip.paintgame.activity.PaintSearchActivity;

public class PaintSearchView extends FingerPaintView implements Animation.AnimationListener{

	private static final float MINP = 0.25f;
	private static final float MAXP = 0.75f;

	public Bitmap mBitmap;
	private Canvas mCanvas;
	private Path mPath;
	//private Context c;
	private Paint mBitmapPaint;
	public Paint mPaint;
	private PaintSearchActivity psActivity;

	public PaintSearchView(Context c, Bitmap bmap, Paint p) {
		super(c, bmap, p);
		//this.c = c;
		if(c instanceof PaintSearchActivity){
			psActivity = (PaintSearchActivity) c;
		}
		mBitmap = bmap;
		mCanvas = new Canvas(mBitmap);
		mPath = new Path();
		mBitmapPaint = new Paint(Paint.DITHER_FLAG);		
		this.mPaint = p;
	}

	public void setBitMap(Bitmap newbmap) {
		mCanvas.drawBitmap(newbmap, 0, 0, mBitmapPaint);
	}


	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
//		if (c instanceof PaintSearchActivity) {
//			((PaintSearchActivity)c).doTouchEvent(event);
//		}
//		return super.onTouchEvent(event);
		if (!psActivity.isFinish()) {
			float x = event.getX();
			float y = event.getY();

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				touch_start(x, y);
				invalidate();
				psActivity.lastTime = System.currentTimeMillis();
				break;
			case MotionEvent.ACTION_MOVE:
				if (!psActivity.startDraw) {
					psActivity.startDraw = true;
					Communicate mycomm = new Communicate(psActivity);
					mycomm.start();
				}
				psActivity.lastTime = System.currentTimeMillis();
				touch_move(x, y);
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				// System.out.println("action up thread id is: " +
				// Thread.currentThread().getId());
				touch_up();
				invalidate();
				psActivity.lastTime = System.currentTimeMillis();
				break;
			}
		}
		return true;
	}

	//@Override
	public void onAnimationEnd(Animation animation) {
		// 展示第一张图
		if (psActivity.file1ok && psActivity.file2ok) {
			// System.out.println("in listener file1ok");
			this.setBitMap(BitmapFactory.decodeFile(psActivity.file1));
			// System.out.println("mAnimation2.setBitMap");
			Animation tmpAnim = AnimationUtils.loadAnimation(psActivity,
					R.anim.myanimation2);
			this.setAnimation(tmpAnim);
			tmpAnim.setAnimationListener(this);
			// System.out.println("mAnimation2 2");
			this.invalidate();
			// System.out.println("mAnimation2 end");
			psActivity.file1 = null;
			psActivity.file1ok = false;
			return;

		} else if (psActivity.file2ok) {// 展示第二张图
			if (psActivity.file2 == null) {// 完成了第二张图展示
				psActivity.file2ok = false;
				psActivity.loopCount = 0;// 循环等待清零
				// //增加reset 按钮
				psActivity.setButtonsVisible(View.VISIBLE);
				return;
			}
			// System.out.println("in listener file2ok 1");
			this.setBitMap(BitmapFactory.decodeFile(psActivity.file2));
			// System.out.println("mAnimation3.setBitMap");
			Animation tmpAnim = AnimationUtils.loadAnimation(psActivity,
					R.anim.myanimation3);
			this.setAnimation(tmpAnim);
			tmpAnim.setAnimationListener(this);
//			String text = new File(psActivity.file2).getName();
//			psActivity.votePic = text;// get current vote pic name
//			if (psActivity.currentService == PGUtil.DRAW_SEARCH) {
//				Toast.makeText(psActivity, text, Toast.LENGTH_SHORT).show();
//			}
			this.invalidate();
			psActivity.file2 = null;

//			//临时测试记录日志 add  by sunlian 
//			try {
//				UserProfile.userRecord();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				psActivity.popupException("");
//				e.printStackTrace();
//			}
			
			return;
		} else {// 循环等待
			psActivity.loopCount++;
			// 循环10次则退出
			if (psActivity.loopCount > 12) {
				psActivity.loopCount = 0;
				return;
			}
			// 判断失败的返回
			if (psActivity.exceptionBackType == PGUtil.BC_SYS_ERR) {
				String text = "系统暂停服务，请稍候再试。。。";
				Toast.makeText(psActivity, text, Toast.LENGTH_SHORT).show();
				psActivity.loopCount = 0;
				return;
			} 
//			else if (psActivity.exceptionBackType == PGUtil.NO_SUIT_RESULT) {
//				String text = "没有找到合适的结果，请重画一个试试看。。。";
//				Toast.makeText(psActivity, text, Toast.LENGTH_SHORT).show();
//				psActivity.loopCount = 0;
//				return;
//			}
			psActivity.exceptionBackType = 0;// 初始化失败返回标志位

			Animation tmpAnim = AnimationUtils.loadAnimation(psActivity,
					R.anim.myanimation2);
			this.setAnimation(tmpAnim);
			tmpAnim.setAnimationListener(this);
			// System.out.println("mAnimation0 2");
			this.invalidate();
			return;
		}

	}

	//@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	//@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}
	
}
